import {defineStore} from "pinia";

// @ts-ignore
export const useStore = defineStore('storeSystem', {
    state: () => {
        return {
            theme: '',
            sideTheme: 'dark',
            isLogin: false,
            token: '',
            user: {},
            showSettings: false,
            layoutSize: '',
            routerHistory: [
                {fullPath: '/console', meta: {title: '控制台'}, closable: false}
            ]
        }
    },
    persist: {
        enabled: true,
        strategies: [{storage: localStorage}],
    },
    getters: {
        getSideTheme: (state) => state.sideTheme,
        getTheme: (state) => state.theme,
        getIsLogin: (state) => state.isLogin,
        getToken: (state) => state.token,
        getUser: (state) => state.user,
        getShowSettings: (state) => state.showSettings,
        getLayoutSize: (state) => state.layoutSize,
        getRouterHistory: (state) => state.routerHistory
    },
    actions: {
        setSideTheme(sideTheme: string) {
            this.sideTheme = sideTheme
        },
        setTheme(color: string) {
            this.theme = color;
        },
        setUser(user: any) {
            this.user = user;
        },
        setLogin(token: string) {
            this.isLogin = true;
            this.token = token;
        },
        setLogout() {
            this.isLogin = false;
            this.token = '';
            this.routerHistory = [
                {fullPath: '/console', meta: {title: '控制台'}, closable: false}
            ];
            this.user = {};
        },
        setShowSettings(showSettings: boolean) {
            this.showSettings = showSettings
        },
        setLayoutSize(layoutSize: string) {
            this.layoutSize = layoutSize
        },
        addRouterHistory(route: any) {
            // @ts-ignore
            this.routerHistory.push(route);
        },
        removeRouterHistory(routeName: string) {
            let index = this.routerHistory.findIndex(item => {
                if (item.fullPath === routeName) {
                    return true
                }
            })
            this.routerHistory.splice(index, 1)
        },
    }
})
